package com.expert.miniapp.service.impl;

import com.expert.miniapp.dao.TechAchievement;
import com.expert.miniapp.dao.TechAchievementMapper;
import com.expert.miniapp.service.TechAchievementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 科技成果业务逻辑实现类
 */
@Service
public class TechAchievementServiceImpl implements TechAchievementService {

    @Autowired
    private TechAchievementMapper techAchievementMapper;

    @Override
    public TechAchievement getAchievementById(String achievementId) {
        if (achievementId == null || achievementId.trim().isEmpty()) {
            return null;
        }
        return techAchievementMapper.findById(achievementId);
    }

    @Override
    public List<TechAchievement> getAchievementsByCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            return techAchievementMapper.findAll();
        }
        return techAchievementMapper.findByCategory(category);
    }

    @Override
    public PageInfo<TechAchievement> getAllAchievements(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<TechAchievement> achievements = techAchievementMapper.findAll();
        return new PageInfo<>(achievements);
    }

    @Override
    public PageInfo<TechAchievement> getRecommendedAchievements(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<TechAchievement> achievements = techAchievementMapper.findByWeightOrder();
        return new PageInfo<>(achievements);
    }

    @Override
    public PageInfo<TechAchievement> searchAchievements(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<TechAchievement> achievements = techAchievementMapper.searchByKeyword(keyword);
        return new PageInfo<>(achievements);
    }
}